<template>
    <popup
        ref="popupRef"
        width="80%"
        :async="true"
        title='新增入库单'
        customClass="bg-page"
        @close="handleClose"
        @confirm="handleConfirm"
    >
        
        <el-card class="!border-none " shadow="never">
            <el-form class="-mb-4" :model="formData" label-width="140px" inline>
                
                <el-form-item label="请选择仓库名称" prop="channel">
                    <el-select class="w-64" v-model="formData.warehouseType">
                        <el-option
                            v-for="item in AfterSaleWarehouseConfigEnum"
                            :label="item.name"
                            :value="item.code"/>
                    </el-select>
                </el-form-item>
            </el-form>
        
        </el-card>
        
        
        <el-card class="!border-none  mt-4 " shadow="never">
            <div class=" my-6  ml-10 w-80 h-11 flex items-center justify-center rounded	 border	"
                 style="background: #99cfe7">
                <ScSignSvgRound size="13" value="您可以通过扫码枪扫描SN入库,直接扫描"></ScSignSvgRound>
            </div>
            
            <div class="my-6">
                <el-input v-model="scanningSn"
                          class=" ml-10 min-h-min h-14 min-w-0 w-10/12 rounded-r-lg"
                          placeholder="您可以通过扫码枪扫描SN入库,直接扫描">
                </el-input>
                
                <el-button
                    class="h-14 w-36 -ml-1 "
                    type="primary"
                    @click="checkItemLineOther"
                    @keyup.enter="checkItemLineOther"
                >
                    添加
                </el-button>
            
            
            </div>
            
            <el-table
                ref="tableRef"
                class="mt-4 mx-10 mb-20 rounded-t-lg"
                size="default"
                :data="formData.afterSaleItemMaterials"
            
            >
                
                <el-table-column type="index" label="行号" width="70"/>
                
                <el-table-column label="售后69码" prop="batch" min-width="110"/>
                <el-table-column label="售后物料编码" prop="itemCode" min-width="110"/>
                <el-table-column label="售后物料名称" prop="itemName" min-width="110"/>
                <el-table-column label="售后SN" prop="sn" min-width="110"/>
                <el-table-column label="入库仓" prop="warehouse" min-width="110"
                                 :formatter="formatterManager.formatterWarehouse"/>
                <el-table-column label="物料实况" prop="fault" min-width="110"/>
                
                <el-table-column label="操作" width="160" fixed="right">
                    <template #default="{ row }">
                        <el-button
                            @click="removeCurrentItemLine(row)"
                            type="primary"
                            link
                        >
                            移除
                        </el-button>
                    
                    
                    </template>
                </el-table-column>
            
            </el-table>
        
        </el-card>
    
    
    </popup>


</template>

<script lang='ts' setup>


import Popup from "@/components/popup/index.vue";
import {UnwrapNestedRefs} from "@vue/reactivity";
import {FormInstance} from "element-plus";
import CooperateOrderLineDTO from "@/api/afterSale/cooperate/dto/CooperateOrderLineDTO";
import AfterSaleCreateWarehouseRequest from "@/api/afterSale/warehouse/request/AfterSaleCreateWarehouseRequest";
import feedback from "@/utils/feedback";
import AfterSaleItemMaterialRequest from "@/api/afterSale/order/request/AfterSaleItemMaterialRequest";
import {getAfterSaleOrderReturnLineBySN} from "@/api/afterSale/order/AfterSaleOrderLineController";
import AfterSaleOrderLineQueryRequest from "@/api/afterSale/order/request/AfterSaleOrderLineQueryRequest";
import {useAfterSaleEnumManager} from "@/manager/base/AfterSaleEnumManager";
import {useFormatterManager} from "@/manager/base/FormatterManager";
import {createWarehouseOrder} from "@/api/afterSale/warehouse/AfterSaleWarehouseController";
import {AfterSaleWarehouseConfigEnum} from "@/config/enums/aftersale/AfterSaleWarehouseEnum";



let afterSaleEnumManager = useAfterSaleEnumManager();
let formatterManager = useFormatterManager(afterSaleEnumManager);


const scanningSn = ref('')
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const formRef = shallowRef<FormInstance>()


// 定义请求
const formData: UnwrapNestedRefs<AfterSaleCreateWarehouseRequest> = reactive(new AfterSaleCreateWarehouseRequest())


/**
 * 弹窗打开
 */
function open(item: CooperateOrderLineDTO) {
    popupRef.value?.open()
}


/**
 *     确认提交
 *
 * @author 徐寿春
 * 2023/4/8 u
 */
async function handleConfirm() {
    createWarehouseOrder(formData).then(() => {
        feedback.notifySuccess("入库成功")
        popupRef.value.close()
        emit('confirm')
    })
    
    
}


/**
 *  弹窗关闭
 *
 * @author 徐寿春
 * 2023/4/8 13:28
 */
function handleClose() {
    formData.afterSaleItemMaterials = []
    emit('close')
    
}


/**
 * 移除当前物料行
 *
 * @author 徐寿春
 * 2023/4/18 11:08
 */
function removeCurrentItemLine(row: AfterSaleItemMaterialRequest) {
    formData.afterSaleItemMaterials = formData.afterSaleItemMaterials.filter(item => item.afterSaleOrderLineId != row.afterSaleOrderLineId)
}

/**
 *  搜索单值处理
 *
 * @author 徐寿春
 * 2023/4/18 11:07
 */
function checkItemLineOther() {
    if (!formData.warehouseType) {
        feedback.notifyWarning("请选择入库仓")
        return
    }
    
    if (!!scanningSn.value) {
        let afterSaleOrderLineQueryRequest = new AfterSaleOrderLineQueryRequest();
        afterSaleOrderLineQueryRequest.sn = scanningSn.value
        afterSaleOrderLineQueryRequest.warehouseType = formData.warehouseType
        
        getAfterSaleOrderReturnLineBySN(afterSaleOrderLineQueryRequest).then(response => {
                let find = formData.afterSaleItemMaterials.find(item => item.afterSaleOrderLineId === response.afterSaleOrderLineId);
                if (!find) {
                    formData.afterSaleItemMaterials.push(response)
                } else {
                    feedback.notifyWarning("当前物料已存在，请勿重复添加")
                    
                }
            }
        )
        
    } else {
        feedback.notifyWarning("请输入物料sn")
    }
    
    
}


const emit = defineEmits(['confirm', 'close'])
defineExpose({
    open
})

</script>

